package com.fangcun.common.task.order;

import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import com.fangcun.common.task.order.config.OrderTimerConfig;
import com.fangcun.service.api.OrderApiService;

/**
 * @作者： 陳曉鬆 @时间： 2018年5月13日 - 上午8:43:17 @description：->
 */
@Component
@EnableAsync
@EnableScheduling
public class OrderTimer implements SchedulingConfigurer {

	// private static Logger LOG = LoggerFactory.getLogger(OrderTimer.class);

	@Autowired
	private OrderTimerConfig orderTimerConfig;

	@Resource(name = "orderApiService")
	private OrderApiService orderApiService;

	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

		// 这里设置的线程池是corePoolSize也是很关键了，自己根据业务需求设定
		taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));

		if (!StringUtils.isEmpty(orderTimerConfig.getInvalidOrderFrequency())) {
			taskRegistrar.addCronTask(new Runnable() {
				@Override
				public void run() {
					orderApiService.invalidOrderCancel();
				}
			}, orderTimerConfig.getInvalidOrderFrequency());
		}

	}

}
